﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for UserMailbox
/// </summary>
public class UserMailbox
{
    private Up1VNDataContext db;
	public UserMailbox()
	{
        db = new Up1VNDataContext();
	}
    public List<IMailboxRepository> GetInbox(int userId)
    {
        return (from o in db.MailBoxes
                where o.Users_Id == userId && o.MailBox_Type == (short)Mailbox.Inbox
                select new IMailboxRepository
                { 
                    Id = (int)o.MailBox_Id,
                    Subject = o.MailBox_Subject,
                    Content = o.MailBox_Content,
                    Status = o.MailBox_Status,
                    From = o.MailBox_FromMail,
                    To = o.MailBox_ToMail,
                    ReceiveDate = o.MailBox_ReceiveDate,
                    ReadDate = o.MailBox_ReadDate,
                    CreateDate = o.MailBox_CreatedDate,
                    ParentId = o.MailBox_ParentId,
                    Type = o.MailBox_Type,
                    UserId = (int)o.Users_Id,
                    username = o.User.Users_FullName,
                    UserEmail = o.User.Users_Email
                }).ToList();
    }
    public List<IMailboxRepository> GetOutbox(int userId)
    {
        return (from o in db.MailBoxes where o.Users_Id == userId && o.MailBox_Type == (short)Mailbox.Outbox select new IMailboxRepository {
            Id = (int)o.MailBox_Id,
            Subject = o.MailBox_Subject,
            Content = o.MailBox_Content,
            Status = o.MailBox_Status,
            From = o.MailBox_FromMail,
            To = o.MailBox_ToMail,
            ReceiveDate = o.MailBox_ReceiveDate,
            ReadDate = o.MailBox_ReadDate,
            CreateDate = o.MailBox_CreatedDate,
            ParentId = o.MailBox_ParentId,
            Type = o.MailBox_Type,
            UserId = (int)o.Users_Id,
            username = o.User.Users_FullName,
            UserEmail = o.User.Users_Email
        }).ToList();
    }

    public MailBox GetEmailById(int userId, int mailboxId)
    {
        return (from o in db.MailBoxes where o.Users_Id == userId && o.MailBox_Id == mailboxId select o).SingleOrDefault();
    }

    public bool SendMail(int userId, string subject, string content, int status, string sendTo, DateTime DateSend, int Type)
    {
        try
        {
            MailBox M = new MailBox();
            M.MailBox_Subject = subject;
            M.MailBox_Content = content;
            M.MailBox_Status = (short)status;
            M.MailBox_ToMail = sendTo;
            M.MailBox_CreatedDate = DateSend;
            M.MailBox_Type = (short)Type;
            db.MailBoxes.InsertOnSubmit(M);
            db.SubmitChanges();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool DeteteMailbox(int userId, int mailboxId)
    {
        try
        {
            var a = (from o in db.MailBoxes where o.Users_Id == userId && o.MailBox_Id == mailboxId select o).SingleOrDefault();
            if (a != null)
            {
                db.MailBoxes.DeleteOnSubmit(a);
                db.SubmitChanges();
                return true;
            }
            else
                return false;
        }
        catch
        {
            return false;
        }
    }
    public bool ComposeMail(int userId, string Title, string Content, int SenderId, int status, int mailboxStyle, DateTime CreateDate, string MailBox_FromMail, DateTime MailBox_ReceiveDate, int ParentId)
    {
        try
        {
            MailBox M = new MailBox();
            M.Users_Id = userId;
            M.MailBox_Subject = Title;
            M.MailBox_Content = Content;
            M.MailBox_FromMail = MailBox_FromMail;
            M.MailBox_Status = (short)status;
            M.MailBox_Type = (short)mailboxStyle;
            M.MailBox_CreatedDate = CreateDate;
            M.MailBox_ReceiveDate = MailBox_ReceiveDate;
            M.MailBox_ParentId = ParentId;
            M.Users_Id = userId;
            db.MailBoxes.InsertOnSubmit(M);
            db.SubmitChanges();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool ReplyMail(int userId, string Title, string Content, int SenderId, int status, int mailboxStyle, DateTime CreateDate, string MailBox_FromMail, DateTime MailBox_ReceiveDate, int ParentId)
    {
        try
        {
            MailBox M = new MailBox();
            M.Users_Id = userId;
            M.MailBox_Subject = Title;
            M.MailBox_Content = Content;
            M.MailBox_FromMail = MailBox_FromMail;
            M.MailBox_Status = (short)status;
            M.MailBox_Type = (short)mailboxStyle;
            M.MailBox_CreatedDate = CreateDate;
            M.MailBox_ReceiveDate = MailBox_ReceiveDate;
            M.MailBox_ParentId = ParentId;
            M.Users_Id = userId;
            db.MailBoxes.InsertOnSubmit(M);
            db.SubmitChanges();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public MailBox GetReplyMail(int MailId)
    {
        return (from o in db.MailBoxes where o.MailBox_Id == MailId select o).SingleOrDefault();
    }
}